package com.dayatang.android.Db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dayatang.android.Model.SystemMessage;

public class SystemMessageServiceDb {
	
	private DBOpenHelper dbOpenHelper;
	
	public SystemMessageServiceDb(Context context){
		this.dbOpenHelper = new DBOpenHelper(context);
	}
	
	public void save(SystemMessage systemMessage){
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put("title", systemMessage.getTitle());
		values.put("content", systemMessage.getContent());
		values.put("organization_name", systemMessage.getOrganizationName());
		values.put("release_date", systemMessage.getReleaseDate());
		values.put("thisid", systemMessage.getThisid());
		values.put("already_read", systemMessage.getAlreadyRead());
		db.insert("system_messages", null, values);
		
		db.close();
	}

	public List<SystemMessage> findSM(String selection , String[] selectionArgs){
		
		List<SystemMessage> systemMessages = new ArrayList<SystemMessage>();
		
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = db.query("system_messages", null, selection, selectionArgs, null, null, "id desc");
		
		while(cursor.moveToNext()){
			
			SystemMessage systemMessage = new SystemMessage();
			
			systemMessage.setId(cursor.getLong(cursor.getColumnIndex("id")));
			systemMessage.setTitle(cursor.getString(cursor.getColumnIndex("title")));
			systemMessage.setContent(cursor.getString(cursor.getColumnIndex("content")));
			systemMessage.setOrganizationName(cursor.getString(cursor.getColumnIndex("organization_name")));
			systemMessage.setReleaseDate(cursor.getString(cursor.getColumnIndex("release_date")));
			systemMessage.setThisid(cursor.getString(cursor.getColumnIndex("thisid")));
			systemMessage.setAlreadyRead(cursor.getString(cursor.getColumnIndex("already_read")));
			
			systemMessages.add(systemMessage);
		}
		cursor.close();
		db.close();
		
		return systemMessages;
	}

	public void delBySelection(String selection,String[] selectionArgs){
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		db.delete("system_messages", selection, selectionArgs);
		
		db.close();
	}
	
	public void updateBySelection(String[] selectionArgs){
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put("already_read", "1");
		
		db.update("system_messages", values, "thisid=?", selectionArgs);
		db.close();
	}
	
	public String getNewId(){
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		Cursor cursor = db.query("newest_id", null, "id=?", new String[]{"1"}, null, null, null);
		
		String id = null;
		
		if(cursor.moveToFirst()){
			id = cursor.getString(cursor.getColumnIndex("newid"));
		}
		
		cursor.close();
		db.close();
	
		return id;
	}
	
	public void updateNewestId(String id){
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put("newid", id);
		
		db.update("newest_id", values, "id=?", new String[]{"1"});
		db.close();
	}
	
}
